﻿using System;
using System.Collections.Generic;
using System.Drawing;
using TigerAndGoats.Control;
using TigerAndGoats.Framework;
using TigerAndGoats.Resources;

namespace TigerAndGoats.Sprites
{
    internal class DestinationTipTiger : DestinationTip
    {
        public DestinationTipTiger()
        {
            this._image = new System.Drawing.Bitmap(@"Resources\destinationTipTiger.png");
        }

        private List<Piece> _viaList = new List<Piece>();
        public new static DestinationTipTiger Empty = new DestinationTipTiger();

        public override void Draw(Graphics graphics)
        {
            if (_location != null)
            {
                graphics.DrawImage(_image, new Rectangle(
                    GetPieceLocation(GetPointByOrdinate(_location.GetOrdinate())
                    , GlobalSettings.PieceSizeTiger())
                    , GlobalSettings.PieceSizeTiger()
                    )
                );
            }
        }

        public void AddViaGoats(List<Piece> reachablePieceOnNodes)
        {
            _viaList.AddRange(reachablePieceOnNodes);
        }

        public override void WalkToHere()
        {
            base.WalkToHere();
            foreach (var viaPiece in _viaList)
            {
                // 中间的羊的所在位置停止显示羊（这些羊脱离了引用，就会被GC自动回收）
                viaPiece.GetLocation().SetPieceEmpty();
            }

            // 判断羊是否被吃光
            int currentGoatNum = 0;
            foreach (var graphNode in ChequerGraph.GetInstance().graphNodes)
            {
                if (graphNode.Value.GetPiece() is Goat)
                {
                    currentGoatNum++;
                    break;
                }
            }

            // 羊被吃光了，则判断老虎胜利
            if (currentGoatNum <= 0)
            {
                GameControl.WinGame(UITipText.TIGER_WIN);
                return; 
            }

            // 老虎走完了，羊走
            GameControl.currentRound = GameControl.Round.Goats;
            
            // 提示羊继续走
            if (GameControl.GoatsOnHand > 0)
            {
                GameControl.SetGeneralTip(UITipText.GAME_CONTINUE_WITH_CLICK_RELEASE_GOATS);
            }
            else
            {
                GameControl.SetGeneralTip(UITipText.GAME_CONTINUE_WITH_MOVE_SINGLE_GOAT);
            }

            ChequerGraph.RefreshUI();
        }
    }
}
